<?php
namespace app\admin\home;
use \Alipay\Alipay;
use app\admin\controller\Admin;
use think\Model;
use think\Request;
class Alipayapi extends Admin
{


    protected $alipay;

    //初始化
    public function __construct()
    {
        $this->alipay = new Alipay();
    }


    /**
     * 二维码拉取支付
     */
    public function ercode(){
        $uid = input('param.id');
        if(empty($uid)){
            apistatus(0,"登录id不能为空");
        }
        $this->checktoken($uid);
        $id = input('param.id');      //vip的id
        if(empty($id)){
            apistatus(0,'付款的id不能为空');
        }else{
            $orderarr = db('home_tops')->where(['id'=>$id])->find();
            if(!$orderarr){
                apistatus(0,'没有找到对应的置顶数据');
            }
        }
        $out_trade_no=order_number();
        $orderarr['out_trade_no']=$out_trade_no;
        $orderarr['uid']=$uid;
        $orderresult = Model('payorder')->saveorder($orderarr);
        if($orderresult) {
            $pcaliay = new Ceshialipy();
            $data['out_trade_no']=$out_trade_no;
            $data['subject']=$orderarr['vipname'];
            $data['total_amount']=$orderarr['price'];
            $alipyresult = $pcaliay->useAliPay($data);
            echo $alipyresult;
            //apistatus(1, "获取成功", $alipyresult);
        }else{
            apistatus(0,"获取支付宝信息失败");
        }
    }




    //二维码查询
    public function queryorder($out_trand_no=''){
        if(empty($out_trand_no)){
            $out_trand_no = input('param.out_trade_no');
        }
        $pcaliay = new Ceshialipy();
        file_put_contents('out_trand.log',$out_trand_no);
        $queryresult = $pcaliay->orderqueryno($out_trand_no);
        file_put_contents('queryorder.log',json_encode($queryresult,true));
        $this->changeOrderinfo($queryresult->out_trade_no,$queryresult->total_amount);
    }


    //APP支付
    public function alipay($order_info)
    {
        $out_trade_no  = $order_info['out_trade_no'];
        //订单名称，必填
        $proName       = $order_info['subject'];
        //付款金额，必填
        $total_amount  = $order_info['total_amount'];
        $orderString = $this->alipay->pay($proName, $out_trade_no, $total_amount);

        $data['url'] = $orderString;
        $data['orderid'] = $out_trade_no;
        $data['type']=1;
        return apistatus(1, "获取成功", $data);

    }



    //退款
    public function refund()
    {
        $price = input('param.price'); //价格

        $out_trade_no = input('param.out_trade_no'); //订单号

        $cancel_reason = input('param.cancel_reason'); //退款理由

        if (!$price) apistatus(0, "退款价格不允许为空", []);

        if (!$out_trade_no) apistatus(0, "唯一订单号不允许为空", []);

        if (!$cancel_reason) apistatus(0, "退款理由不允许为空", []);

        $order_type = explode('_', $out_trade_no);

        if ($order_type[0] == "MJS") {

            AlipayService::refund($out_trade_no, $price, $cancel_reason);


        } else {

            apistatus(0, "当前订单号错误或者该订单不允许退款操作");

        }


    }




    //异步回调 没有作用
    public function returnnotify(Request $request){
        $param = $request->param();
        file_put_contents('alipy.log',json_encode($param));
    }



    //前端支付，同步调用 之前测试
    public function notify1()
    {
        $orderid = input('param.orderid');
        file_put_contents('orderid.log',$orderid);
        $this->queryorder($orderid);
    }




    //退款查询
    public function refundQuery()
    {

        $out_trade_no = input('param.out_trade_no'); //订单号

        if (!$out_trade_no) apistatus(0, "唯一订单号不允许为空", []);

        return $this->alipay->refundQurey($out_trade_no);

    }


    //转账接口
    public function fundTransToAccount()
    {
        $outBizNo = input('param.outBizNo'); //订单号

        $payeeAccount = input('param.payeeAccount'); //支付宝账号

        $amount = input('param.amount'); //金额

        if (!$outBizNo) apistatus(0, "唯一订单号不允许为空", []);

        if (!$payeeAccount) apistatus(0, "支付宝账号不允许为空", []);

        if (!$amount) apistatus(0, "转账金额不允许为空", []);

        return $this->alipay->fundTransToAccount($outBizNo, $payeeAccount, $amount);

    }



    //订单查询 RSA2 dz

    public function notify($orderid='')
    {
        if(empty($orderid)) {
            $orderid = input('param.orderid');
        }
        $param = input('param.');
        file_put_contents('b.log',$param);
        $queryresult=$this->alipay->orderQuery($orderid);
        $result = json_decode($queryresult,true);
        if($result['alipay_trade_query_response']['code'] == 10000){
            $this->changeOrderinfo($result['alipay_trade_query_response']['out_trade_no'],$result['alipay_trade_query_response']['total_amount']);
        }
    }




    //支付成功后操作订单数据表，更改订单状态
    public function changeOrderinfo($out_trade_no,$total_amount){
        $chargeorder = db('home_order')->alias("O")->join('home_charge C','O.buyid=C.id')->where(['O.orderid' =>$out_trade_no])->field('O.id,O.uid,C.count')->find(); //获取当前的订单信息
        //修改vip订单表
        $data['alipay_price'] = $total_amount;
        $data['status'] = 1;
        $current = time();          //当前的时间
        $data['paytime'] = $current;
        $result_order = db('home_order')->where(['id' => $chargeorder['id']])->update($data);
        if ($result_order) {
            $user_data['rd_coin'] = $chargeorder['count'];
            $user_result_order = db('home_users')->where('id', $chargeorder['uid'])->update($user_data);
            if ($user_result_order) {
                apistatus(1, '购买成功');
            } else {
                apistatus(0, '购买失败');
            }
        } else {
            apistatus(0, '没有找到对应的订单信息');
        }
    }


}